# !/usr/bin/env python
# File       : modle.py
# Time       : 2022/8/29 15:11
# Author     : hyx
# -*- coding:utf-8 -*-
import uuid


from sqlalchemy.orm import backref
from sqlalchemy.dialects.postgresql import UUID
from ext import db
from datetime import datetime


# 验证码
class EmailCaptchaModel(db.Model):
    __tablename__ = 'email_captcha'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(100), nullable=False, unique=True)
    captcha = db.Column(db.String(10), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)


def get_uuid():
    get_timestamp_uuid = uuid.uuid1()  # 根据 时间戳生成 uuid , 保证全球唯一
    return get_timestamp_uuid
# 作者，用户
class UserModel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(200), nullable=False)
    email = db.Column(db.String(100), nullable=False, unique=True)
    password = db.Column(db.String(200), nullable=False)
    icon = db.Column(db.String(200), nullable=True)
    join_time = db.Column(db.DateTime, default=datetime.now)
    is_root = db.Column(db.Boolean, default=False)
    user_uuid = db.Column(db.String(200), unique=True, nullable=False,index=True,default=lambda: str(get_uuid()))



# 发布的问答,文章
class QuestionModel(db.Model):
    __tablename__ = 'question'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.LargeBinary(16777216), nullable=False)
    # 解释文章的内容
    explain = db.Column(db.String(200), default=title)
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 文章类别
    category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
    category = db.relationship('CategoryModel', backref='questions', cascade="all,delete")

    # 作者
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    author = db.relationship('UserModel', backref='questions', cascade="all,delete")


# 类别
class CategoryModel(db.Model):
    __tablename__ = 'categories'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), nullable=False, unique=True)


# 问答评论
class AnswerModel(db.Model):
    __tablename__ = 'answer'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # BLOB
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 评论所对应的文章
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
    question = db.relationship('QuestionModel', backref=db.backref('answers', order_by=create_time.desc()),
                               cascade="all,delete")
    # 该评论对应的作者
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    author = db.relationship('UserModel', backref="answers", cascade="all,delete", foreign_keys='AnswerModel.author_id')
    # 对应回复的作者
    to_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    to_user = db.relationship('UserModel', backref="commented", foreign_keys='AnswerModel.to_user_id')

    # 回复表 邻接列表关系
    replies = db.relationship('AnswerModel', backref=backref("replied", remote_side=[id]))
    replied_id = db.Column(db.Integer, db.ForeignKey('answer.id'))
